/*
 * Copyright 2014 Graham Smith
 */
package uk.co.wobblycogs.vegas.scan;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import uk.co.wobblycogs.vegas.project.Project;
import uk.co.wobblycogs.vegas.project.ProjectService;
import uk.co.wobblycogs.vegas.riff.RIFFProject;
import uk.co.wobblycogs.vegas.riff.RIFFProjectFactory;
import uk.co.wobblycogs.vegas.riff.RIFFObject;
import uk.co.wobblycogs.vegas.riff.RIFFType;
import uk.co.wobblycogs.vegas.riff.RIFFUtilities;
import uk.co.wobblycogs.vegas.riff.RIFFResource;

/**
 *
 * @author Graham Smith
 */
public class VegasFinder extends FileFinder {

	public static final String DEFAULT_GLOB_PATTERN = "*.vf";
	private final ProjectService ps = new ProjectService();

	public VegasFinder() {
		this(DEFAULT_GLOB_PATTERN);
	}

	public VegasFinder(String pattern) {
		super(pattern);
	}

	/**
	 * Processes the Vegas files and places the results into the database.
	 *
	 * @param file
	 * @throws java.io.IOException
	 */
	@Override
	public void process(Path file) throws IOException {

		//logger.debug("Processing: {}", file);
		RIFFProjectFactory rpf = new RIFFProjectFactory();
		RIFFProject rp = rpf.load(file);

		List<RIFFObject> header = new ArrayList<>();
		RIFFUtilities.find(rp, header, RIFFType.HEADER.getTypeSignature());

		Project p = new Project(file);

		List<RIFFObject> usedResources = new ArrayList<>();
		RIFFUtilities.find(rp, usedResources, RIFFType.RESOURCE.getTypeSignature());

		List<RIFFResource> usedFileResources = new ArrayList<>();
		for (RIFFObject o : usedResources) {
			RIFFResource r = (RIFFResource) o;
			switch (r.getResourceType()) {
				case FILE:
					usedFileResources.add(r);
					break;
				default:
				//Do nothing.
			}
		}

//		usedFileResources.stream().forEach((r) -> {
//			scanResults.addProjectResult(file, Paths.get(r.getResource()));
//		});
		ps.persist(p);
	}

}
